<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;

class Develop extends Controller
{
    public function button()
    {
        return view();
    }

    public function button_edit()
    {
        //访问页面
        if ($this->request->isGet()) {
            $id = input("id/d",0);
            $data = DB::name('s_button')->where(["id"=>$id])->find();
            $this->assign("data",$data);
            return view();
        }

        //修改保存
        if ($this->request->isPost()) {

            //获取参数
            $id = intval(input("id"));
            $name = input("name");
            $sort = input("sort");
            $category = input("category");
            $action = input("action");
            $tip = input("tip");
            $status = input("status");
            $ico = input("ico");
            $color = input("color");

            //验证数据
            if(empty($name)){
                return json(array('code' => 1,'text'=>'名称不能为空!'));
            }
            if(empty($status)){
                return json(array('code' => 1,'text'=>'状态不能为空!'));
            }

            //拼装数据
            $data = array();
            $data['name'] = $name;
            $data['sort'] = $sort;
            $data['category'] = $category;
            $data['action'] = $action;
//            $data['tip'] = $tip;
            $data['status'] = $status;
            $data['ico'] = $ico;
            $data['color'] = $color;

            if($id > 0){
                DB::name("s_button")->where(["id"=>$id])->update($data);
            } else {
                DB::name("s_button")->insert($data);
            }

            return json(array('code' => 0,'text'=>'操作成功!'));
        }
    }

    public function button_lst(){
        $status = input("status");
        $key = input("key");

        $map = array();
        if(!empty($status)){
            $map["status"] = $status;
        }
        if(!empty($key)){
            $map["name"] = ["like","%$key%"];
        }

        //分页
        $offset = input("offset/d",0);
        $limit = input("limit/d",20);

        $count = DB::name('s_button')->where($map)->count();
        $data = DB::name('s_button')->where($map)->limit($offset,$limit)->order('category,sort')->select();

        $ret = array();
        $ret['total'] = $count;
        $ret['rows'] = ($data);

        return json($ret);
    }

    public function button_del(){
        $id = input("id/d",0);

        DB::name("s_button")->where(array("id"=>$id))->delete();

        return json(array('code' => 0,'text'=>'操作成功!'));
    }


    public function menu()
    {
        return view();
    }

    public function menu_edit()
    {
        //访问页面
        if ($this->request->isGet()) {
            $id = input("id/d",0);
            $data = DB::name('s_menu')->where(["id"=>$id])->find();

            $lst = DB::name('s_menu')->where(["id"=>['>',0]])->select();
            $lst = get_tree_data($lst,'id','pid','title');

            $this->assign("plist",$lst);
            $this->assign("data",$data);
            return view();
        }

        //修改保存
        if ($this->request->isPost()) {

            //获取参数
            $id = intval(input("id"));
            $title = input("title");
            $sort = input("sort");
            $pid = input("pid");
            $url = input("url");
            $status = input("status");
            $ico = input("ico");

            //验证数据
            if(empty($title)){
                return json(array('code' => 1,'text'=>'名称不能为空!'));
            }
            if(empty($status)){
                return json(array('code' => 1,'text'=>'状态不能为空!'));
            }

            //拼装数据
            $data = array();
            $data['title'] = $title;
            $data['sort'] = $sort;
            $data['pid'] = $pid;
            $data['url'] = $url;
            $data['status'] = $status;
            $data['ico'] = $ico;

            if($id > 0){
                DB::name("s_menu")->where(["id"=>$id])->update($data);
            } else {
                DB::name("s_menu")->insert($data);
            }

            return json(array('code' => 0,'text'=>'操作成功!'));
        }
    }

    public function menu_lst(){
        $from = input('from');
        $user = get_user();
        $role_id = $user['role_id'];
        $user_id = $user['id'];
        $data = [];
        if($user['account'] !== 'admin')
        {
            $data = DB::name('s_menu')
                ->alias('a')
                ->join('s_role_menu_button b','a.id = b.menu_id')
                ->where(['b.role_id'=>$role_id,'b.button_id'=>0])
                ->field('a.*')
                ->order('sort','asc')
                ->union(function($query) use ($user_id){
                    $query->table('s_menu')
                        ->alias('a')
                        ->join('s_user_menu_button c','a.id = c.menu_id')
                        ->where(['c.user_id'=>$user_id,'c.button_id'=>0])
                        ->field('a.*');
                })
                ->select();
        }
        else{
            $data = DB::name('s_menu')
                ->where($map)
                ->order('sort','asc')
                ->select();
        }
        return json($data);

    }

    public function menu_del(){
        $id = input("id/d",0);

        DB::name("s_menu")->where(array("id"=>$id))->delete();

        return json(array('code' => 0,'text'=>'操作成功!'));
    }


    public function menu_button()
    {
        return view();
    }
    public function menu_button_lst(){
        $user_id = input('user_id/d',0);
        $menu_id = input('menu_button_id/d',0);
        $role_id = get_user()['role_id'];
        $map = array();
        $user = get_user();
        $map['b.user_id'] = ['in',[$user['id']]];
        $map['b.menu_id'] = $menu_id;
        $data = DB::name('s_button')
            ->alias('a')
            ->join('s_user_menu_button b','a.id = b.button_id')
            ->where($map)
            ->field('a.*')
            ->union(function($query) use ($role_id,$menu_id){
                $query->name('s_button')
                    ->alias('a')
                    ->join('s_role_menu_button c','a.id = c.button_id')
                    ->where(['c.role_id'=>$role_id,'c.menu_id'=>$menu_id])
                    ->field('a.*');
            })
            ->select();
        //给最高权限的人,开启所有的
        if($user['account'] == 'admin')
        {
            $data = DB::name('s_button')
                  ->alias('a')
                  ->join('s_menu_button b','a.id = b.button_id')
                  ->where(['b.menu_id'=>$menu_id])
                  ->field('a.*')
                  ->select();
        }
        //按钮分配的菜单中显示所有按钮
        $type = input('type');
        if($type == '按钮分配')
        {
            $data = DB::name('s_button')
                  ->select();
        }



        return json($data);
    }

    public function menu_button_lst3(){
        $map = array();
        $data = DB::name('s_menu_button')->field('button_id,menu_id')->where($map)->select();
        return json($data);
    }

    public function menu_button_save()
    {
        $data = input('data/a');

        Db::startTrans();
        DB::name('s_menu_button')
             ->where("1 = 1")
             ->delete();
        if(!empty($data)){
            foreach ($data as $v){
                $data = array();
                $data['menu_id'] = $v['menu_id'];
                $data['button_id'] =$v['button_id'];
                DB::name('s_menu_button')->insert($data);
            }
        }
        Db::commit();

        return json(array('code' => 0,'text'=>'操作成功!'));
    }


}
